package com.net;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;
import java.util.List;

/**
 * Created by peak on 2016/1/7.
 */
public class CookieTest {
    public static void main(String[] args) throws Exception{
        CookieManager manager= new CookieManager();
        CookieHandler.setDefault(manager);

        /*URL url = new URL("http://web0.com/setCookie.jsp");
        URLConnection conn = url.openConnection();
        System.out.println(conn.getHeaderFields());

        InputStream in = (InputStream)conn.getContent(new Class[]{InputStream.class});
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String s=null;
        while((s=reader.readLine())!=null){
            System.out.println(s);
        }
        reader.close();
        System.out.println("-------------------------------------");
        CookieStore cs = manager.getCookieStore();
        List<HttpCookie> list =  cs.getCookies();
        if(list!=null){
            for(HttpCookie cookie:list){
                System.out.println(cookie);
            }
        }*/

        CookieStore cs = manager.getCookieStore();
        URI uri=new URI("http://web0.com/");
        System.out.println(uri.getHost());
        HttpCookie cookie = new HttpCookie("xxxx","yesyoa");
        cookie.setDomain("web0.com");
        cookie.setPath("/");
        cs.add(uri,cookie);
        List<HttpCookie> list =  cs.getCookies();
        if(list!=null){
            for(HttpCookie cookie1:list){
                System.out.println(cookie1.getDomain()+" - "+cookie1.getPath());
            }
        }

        URL url = new URL("http://web0.com/getCookie.jsp");
        URLConnection conn = url.openConnection();
         System.out.println(conn.getHeaderFields());

        InputStream in = (InputStream)conn.getContent(new Class[]{InputStream.class});
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String s=null;
        while((s=reader.readLine())!=null){
            System.out.println(s);
        }

        reader.close();
    }
}
